
TOOL.Category		= "Construction"
TOOL.Name 			= "Seller Gun"
TOOL.Command 		= nil
TOOL.ConfigName 	= ""

if ( CLIENT ) then
	language.Add( "Tool_sellgun_name", "SellGun" )
	language.Add( "Tool_sellgun_desc", "Sells a prop for 75% of the original price" )
	language.Add( "Tool_sellgun_0", "Left-Click: Sell single prop. Right-Click: Sell whole contraption. Reload: Check sell value" )
end

function TOOL:LeftClick( trace )

	if (SERVER) then
		if trace.HitNonWorld then --humm we got somthin
			local ent = trace.Entity
			local ply = self:GetOwner()
			local phys = ent:GetPhysicsObject()
			
			if ent:IsValid() then
				if ent:GetClass() == "prop_physics" then -- Normal Prop
					
					local ed = EffectData()
					ed:SetEntity(ent)
					util.Effect( "entity_remove", ed, true, true )
					
					local PropPrice = GetPropPrice(phys:GetVolume(), phys:GetMass())
					
					local MoneyBack = math.ceil((PropPrice/100)*75)
					
					ply:Money_Add(MoneyBack)
					ent:Remove()
					
					ply:PrintMessage(HUD_PRINTTALK,	"Sold prop for: "..tostring(MoneyBack))
				
				elseif((GetPrice(ent:GetClass(), ent:GetModel())) > 0) then --Prolly a CAF item
					
					local price = GetPrice(ent:GetClass(), ent:GetModel())
					
					local ed = EffectData()
					ed:SetEntity(ent)
					util.Effect( "entity_remove", ed, true, true )
					
					local MoneyBack = math.ceil((price/100)*75)
					
					ply:Money_Add(MoneyBack)
					ent:Remove()
					ply:PrintMessage(HUD_PRINTTALK,	"Sold prop for: "..tostring(MoneyBack))

				else
					ply:PrintMessage(HUD_PRINTTALK,	"Not a valid prop.")
				end
			end	
		end
	end
	return true
end

function TOOL:RightClick ( trace )

	if (SERVER) then
		if trace.HitNonWorld then --humm we got somthin
		
			local ply = self:GetOwner()
			local Count = 0
			local TotalPrice = 0
			
			local attached = duplicator.GetAllConstrainedEntitiesAndConstraints(trace.Entity, {},{} ) or {}
			attached[trace.Entity:EntIndex()] = trace.Entity
		
			for _,ent in pairs(attached) do
				if ent:IsValid() then
					local Class = ent:GetClass()
					local Model = ent:GetModel()
					local phys = ent:GetPhysicsObject()
					
					if (Class == "prop_physics") then -- Normal Prop
						
						local ed = EffectData()
						ed:SetEntity(ent)
						util.Effect( "entity_remove", ed, true, true )							
						local PropPrice = GetPropPrice(phys:GetVolume(), phys:GetMass())							
						local MoneyBack = math.ceil((PropPrice/100)*75)							
						ply:Money_Add(MoneyBack)
						ent:Remove()	
						
						Count = Count + 1
						TotalPrice = TotalPrice + MoneyBack
						
					elseif((GetPrice(Class, Model) > 0)) then -- CAF item
						
						local price = GetPrice(Class, Model)							
						local ed = EffectData()
						ed:SetEntity(Ent)
						util.Effect( "entity_remove", ed, true, true )							
						local MoneyBack = math.ceil((price/100)*75)							
						ply:Money_Add(MoneyBack)
						ent:Remove()
						
						Count = Count + 1
						TotalPrice = TotalPrice + MoneyBack

						--Everything Else
					elseif(Class == "prop_ragdoll" || Class == "gmod_thruster" || Class == "gmod_wheel" 
						|| Class == "gmod_thruster" || Class == "gmod_cameraprop" || Class == "gmod_rtcameraprop"
						|| Class == "gmod_turret" || Class == "gmod_lamp" || Class == "gmod_button"
						|| Class == "prop_vehicle_jeep" || Class == "prop_vehicle_prisoner_pod" || Class == "prop_vehicle_airboat"
						|| Class == "gmod_nail" || Class == "gmod_emitter" || Class == "sent_ball"
						|| Class == "npc_rollermine" || Class == "npc_manhack" || Class == "npc_cscanner") then
				
						local ed = EffectData()
						ed:SetEntity(ent)
						util.Effect( "entity_remove", ed, true, true )
					end
				end
			end
			ply:PrintMessage(HUD_PRINTTALK,	"Sold "..tostring(Count).." items for: "..tostring(TotalPrice).." credits")	
		end
	end
	return true
end

function TOOL:Reload ( trace )
	if (SERVER) then
		if trace.HitNonWorld then --humm we got somthin
		
			local ply = self:GetOwner()
			local Count = 0
			local TotalPrice = 0
			
			local attached = duplicator.GetAllConstrainedEntitiesAndConstraints(trace.Entity, {},{} ) or {}
			attached[trace.Entity:EntIndex()] = trace.Entity
		
			for _,ent in pairs(attached) do
				if ent:IsValid() then
					local Class = ent:GetClass()
					local Model = ent:GetModel()
					local phys = ent:GetPhysicsObject()
					
					if (Class == "prop_physics") then -- Normal Prop
						
						local PropPrice = GetPropPrice(phys:GetVolume(), phys:GetMass())
						local MoneyBack = math.ceil((PropPrice/100)*75)
						Count = Count + 1
						TotalPrice = TotalPrice + MoneyBack
						
					elseif((GetPrice(Class, Model) > 0)) then -- CAF item
						
						local price = GetPrice(Class, Model)
						local MoneyBack = math.ceil((price/100)*75)
						Count = Count + 1
						TotalPrice = TotalPrice + MoneyBack

						--Everything Else
					elseif(Class == "prop_ragdoll" || Class == "gmod_thruster" || Class == "gmod_wheel" 
						|| Class == "gmod_thruster" || Class == "gmod_cameraprop" || Class == "gmod_rtcameraprop"
						|| Class == "gmod_turret" || Class == "gmod_lamp" || Class == "gmod_button"
						|| Class == "prop_vehicle_jeep" || Class == "prop_vehicle_prisoner_pod" || Class == "prop_vehicle_airboat"
						|| Class == "gmod_nail" || Class == "gmod_emitter" || Class == "sent_ball"
						|| Class == "npc_rollermine" || Class == "npc_manhack" || Class == "npc_cscanner") then
						local ed = EffectData()
						ed:SetEntity(ent)
						util.Effect( "entity_remove", ed, true, true )
					end
				end
			end
			
			if(Count == 1) then
				ply:PrintMessage(HUD_PRINTTALK,	"Prop value: "..tostring(TotalPrice))
			elseif(Count > 1) then
				ply:PrintMessage(HUD_PRINTTALK,	"Number of props: "..tostring(Count))
				ply:PrintMessage(HUD_PRINTTALK,	"Contraption value: "..tostring(TotalPrice))
			end
		end
	end
	return true
end

function TOOL.BuildCPanel( panel )
	panel:AddControl( "Header", { Text = "Tool_SellGun_name", Description = "Tool_SellGun_desc" } )
end